<template>
  <div class="tableDiv">
    <el-table :data="treatmentRecordDataList" show-summary :summary-method="getSummaries" border>
      <el-table-column label="序号" align="center" type="index" width="50" sortable>
        <template slot-scope="scope">
          <span>{{ getTreatmentRecordIndex(scope.$index) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="顾客编号" align="center" prop="visitNum" sortable width="160" />
      <el-table-column label="顾客姓名" align="center" prop="patientName" :show-overflow-tooltip="true" width="100"
        sortable />
      <el-table-column sortable label="治疗日期" align="center" prop="consultationDate" width="155">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.treatmentTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="医生诊断" align="center" prop="doctorDiagnosis" sortable width="220" />
      <el-table-column sortable label="治疗医生" align="center" prop="treatmentDoctorName" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.treatmentDoctorName ? scope.row.treatmentDoctorName : scope.row.treatmentDoctor }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" :show-overflow-tooltip="true" width="120px" sortable>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_diagnosis_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="治疗项目" align="center" prop="packageContent" sortable width="100" />
      <el-table-column sortable label="治疗部位" align="center" prop="treatmentPlace" width="100">
      </el-table-column>
      <el-table-column sortable label="治疗价格" align="center" prop="treatmentPrice" width="100" />

      <el-table-column label="治疗次数" align="center" prop="treatmentTimes" sortable width="100">
        <template slot-scope="scope">
          <span>1</span>
        </template>
      </el-table-column>

      <el-table-column sortable label="治疗年龄" align="center" prop="treatmentAge" :show-overflow-tooltip="true"
        width="100" />
      <el-table-column sortable label="治疗反应" align="center" prop="treatmentReaction" width="100">
        <template slot-scope="scope">
          <span>{{ getTreatmentReactionText(scope.row.treatmentReaction) }}</span>
          <!-- <dict-tag :options="dict.type.sys_treatment_reaction" :value="scope.row.treatmentReaction" /> -->
        </template>
      </el-table-column>
      <el-table-column sortable label="治疗效果" align="center" prop="treatmentEffect" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ys_treatment_seffect" :value="scope.row.treatmentEffect" />
        </template>
      </el-table-column>

      <el-table-column label="登记日期" align="center" prop="createTime" width="180" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="治疗参数" align="center" prop="remark" sortable width="220" />
    </el-table>
    <pagination v-show="treatmentRecordTotal > 0" :total="treatmentRecordTotal"
      :page.sync="treatmentRecordQueryParams.pageNum" :limit.sync="treatmentRecordQueryParams.pageSize"
      @pagination="getTreatmentList" />
  </div>
</template>

<script>
import store from "@/store";
import { getTreatmentList } from "@/api/treatmentWorkstation/treatmentManagement";
export default {
  name: "treatmentHistory",
  components: {},
  props: ["basicInfo"],
  dicts: ['sys_diagnosis_status',
    'sys_user_sex', "photo_upload_type",
    "photo_lighting_type",
    "sys_face_part",
    "sys_patient_properties",
    "sys_skin_tone", "sys_patient_sort",
    'sys_treatment_reaction', 'sys_treatment_effect'],
  data() {
    return {
      treatmentRecordDataList: [],
      treatmentRecordQueryParams: {
        pageNum: 1,
        pageSize: 10,
        visitNum: '',
        patientId: '',
      },
      treatmentRecordTotal: 0,
    };
  },
  created() {

  },
  mounted() {
    console.log(this.basicInfo)
    if (this.basicInfo) {
      this.treatmentRecordQueryParams.patientId = this.basicInfo.patientId
      this.getTreatmentList()
    }

  },
  methods: {
    getTreatmentReactionText(value) {
      let text = ''
      if (value.includes(',')) {
        let data = value.split(',')
        data.forEach((item, index) => {
          this.dict.type.sys_treatment_reaction.forEach((row) => {
            if (item === row.value) {
              if (index + 1 < data.length) {
                text = `${text + row.label},`
              } else {
                text += row.label
              }
            }
          })
        })
      } else {
        this.dict.type.sys_treatment_reaction.forEach((row) => {
          if (value === row.value) {
            text = row.label
          }
        })
      }
      return text
    },
    getTreatmentRecordIndex(index) {
      return (
        (this.treatmentRecordQueryParams.pageNum - 1) * this.treatmentRecordQueryParams.pageSize + index + 1
      );
    },
    // 获取顾客治疗列表
    getTreatmentList() {
      getTreatmentList(this.treatmentRecordQueryParams).then(
        (response) => {
          if (response.code == 200) {
            this.treatmentRecordDataList = response.rows;
            this.treatmentRecordTotal = response.total
          } else {
            this.$modal.msgError(response.msg);
          }

        }
      )
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      console.log('getSummaries---', param)
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (column.label == '治疗价格') {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] += ' 元';
        } else {
          sums[index] = '';
        }
      });

      return sums;
    }

  },
};
</script>
<style scoped lang="scss">
.tableDiv {
  min-height: 90vh;
}
</style>
